#include "tc_man.h"
#include "tc_thread.h"
#include "../util/threadtimer.h"
#include "../util/timer.h"

void tc_Sleep(uint32 seconds)
{
	struct timespec ts, rmts;
	
	ts.tv_sec = seconds/1000;
	ts.tv_nsec = (seconds%1000)*1000000;
	while(1)
	{
		int ret = nanosleep(&ts, &rmts);
		if(ret == 0) 
		{
			break;
		}

		int sleepErr = errno;
		if(ret == -1 && sleepErr == EINTR)
		{
			ts = rmts;
		}
	}
}

tc_thread::tc_thread(tc_sink* sink)
{
	func_lock s_lock;
	m_sink = sink;
	m_single = new epoll_single(20);
	m_log = new logfile("tc_thread");
}

tc_thread::~tc_thread()
{
	func_lock s_lock;
	delete m_single;
	delete m_log;
}

bool tc_thread::add_socket(uint32 socket)
{
	func_lock s_lock;
	if(socket == -1)
		return false;
	
	char tmp_str[1023] = {0};
	sprintf(tmp_str,"add_socket error, socket=%d",socket);
	
	if(m_single->set(socket,EPOLLIN) ==false)
        m_log->WriteTrace(tmp_str);

	 return true;
}

bool tc_thread::del_socket(uint32 socket)
{
	func_lock s_lock;
   if(socket == -1)
	   return true;

   bool rt =  m_single->del(socket);

   if(rt == false)
	   m_log->WriteTrace("delete single error");

   return rt;
}

void tc_thread::Execute()
{
	printf("tc_thread_client is run\r\n");
	uint32 count;
	uint32 m_count=0;
	struct epoll_event events[MAXEPOLLSIZE];
	char  buf[16*1024];
	uint32 buf_size =0;
	while(!m_finished)
	{
		m_count ++;
		
		if(m_count % 5000 == 0)
		{
			char tt[1024] = {0};
			sprintf(tt,"m_count=%d",m_count);
			m_log->WriteTrace(tt);
		}

		count = m_single->wait(events);

		m_sink->OnSend();
		
		if(count == 0)
			continue;
		
		for(uint32 i=0; i<count ; i++)
		{  
			int size = 1;
			while(size > 0)
			{	
				uint32 time = get_time();
				if(m_sink->OnBuffer(events[i].data.fd) == true)
				{
					memset(buf,0,16*1024);
					buf_size = 16*1024;
					uint32 recv_time = get_time();
					
					size = recv(events[i].data.fd,buf,buf_size,0) ;
					
					if(get_time() -recv_time > 0)
					{
						char llr[1023] = {0};
						sprintf(llr,"client recv time=%d,time=%d",
							get_time() -recv_time,get_time());
						m_log->WriteTrace(llr);
					}

					if(size == 0)
					{
						uint32 error_time = get_time();
						char ll[1024] = {0};
						sprintf(ll,"client close,socket =%d",events[i].data.fd);
						m_log->WriteTrace(ll);
						m_single->del(events[i].data.fd);
						m_sink->OnError(events[i].data.fd);

						if(get_time() -error_time > 0)
						{
							char llr[1023] = {0};
							sprintf(llr,"client recv time=%d,time=%d",
								get_time() -error_time,get_time());
							m_log->WriteTrace(llr);
						}
						break;
					}
					
					if(size < 0)
					{
						m_log->WriteTrace("error recv");
						m_sink->OnError(events[i].data.fd);
						break;
					}

					uint32 time=get_time();
					m_sink->OnRead(events[i].data.fd,buf,size);

					if(get_time() -time)
					{
						char lle[1023] = {0};
						sprintf(lle,"client read time=%d,time=%d",
							get_time() -time,get_time());
						m_log->WriteTrace(lle);
					}
					
					break;
				}
				
				if(get_time() -time > 0)
				{
					char lle[1023] = {0};
					sprintf(lle,"on buffer time=%d,time=%d",
						get_time() -time,get_time());
					m_log->WriteTrace(lle);
				}

				break;
			}
		}
	}

	return;
}
	
